const app = getApp()
const api = require('../../server/Api.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    lat: 0,
    lng: 0,
    addressName: "",
    shops: [], //店铺
    curPage: 1,
    pageSize: 6,
    hasMore: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getLocation();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  queryShops: function(noResetPage) {
    if (!noResetPage) {
      this.setData({
        curPage: 1,
        hasMore: true
      })
    }
    let that = this;
    let params = {
      latitude: that.data.lat,
      longitude: that.data.lng,
      pageNumber: that.data.curPage,
      pageSize: that.data.pageSize,
      platId: app.globalData.platformId
    }
    // tt.showNavigationBarLoading();
    api.queryShops(params).then(res => {
      console.log("获取到最近店铺", res);
      for (let i = 0; i < res.records.length; i++) {
        res.records[i].fans = res.records[i].fans || "0"
        res.records[i].buyExponent = app.setBuyExponent(res.records[i].buyExponent)
        res.records[i].distance = app.setMtoKm(res.records[i].distance);
        if (res.records[i].images && res.records[i].images !== "") {
          res.records[i].images = app.resetImageUrl(res.records[i].images, 200, 200)
        } else {
          res.records[i].images = "../../images/logo1.png";
        }
      }

      let records = res.records;
      let maxPage = parseInt(res.pages);
      var datas = that.data.curPage === 1 ? [] : that.data.shops;
      if (records) {
        datas = datas.concat(records);
      }
      that.setData({
        hasMore: maxPage > that.data.curPage,
        shops: datas,
        curPage: that.data.curPage + 1,
      })
      console.log(that.data.shops)
      // tt.hideNavigationBarLoading();
    }).catch(msg => {
      let message = typeof msg == "string" ? msg : msg.message;
      app.showToast(message);
      // tt.hideNavigationBarLoading();
    })
  },

  //上拉加载
  onReachBottom: function() {
    if (this.data.hasMore == false) return;
    this.queryShops(true);
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  getLocation() {
    let that = this;
    tt.getLocation({
      type: 'gcj02',
      success: res => {
        that.setData({
          lat: res.latitude,
          lng: res.longitude,
          addressName: "当前位置"
        })
        this.queryShops();
        app.globalData.qqmapsdk.reverseGeocoder({
          location: {
            latitude: res.latitude,
            longitude: res.longitude
          },
          success: function(res) {
            that.setData({
              addressName: res.result.formatted_addresses.recommend
            })
          },
          fail: function(error) {
            console.log("获取地理位置失败", error)
          }
        })
      }
    })
  },
  //选中店铺
  toSelectAdd: function(e) {
    let item = e.currentTarget.dataset.item;
    console.log(item);
    app.globalData.lat = this.data.latitude;
    app.globalData.lng = this.data.longitude;
    app.globalData.addressName = this.data.addressName;
    app.globalData.shopId = item.id;
    tt.navigateBack({
      delta: 1,
    })
  },
  chooseByMap: function() {
    let that = this;
    tt.chooseLocation({
      success: function(res) {
        console.log(res)
        if (res.name != "") {
          that.setData({
            lat: res.latitude,
            lng: res.longitude,
            addressName: res.name
          })
          that.queryShops();
        }
      },
    })
  }

})